<template>
	<view>
		<view class="main">
			<u-form :model="form" ref="uForm" label-width="160">
				<u-form-item label="*头像">
					<u-avatar :src="src" size="120"></u-avatar>
				</u-form-item>
				<u-form-item label="*姓名">
					<u-input v-model="form.nickName"></u-input>
				</u-form-item>
				<u-form-item label="预约时间" prop="birthday">
					<u-input v-model="form.birthday"></u-input>
				</u-form-item>
				<u-form-item label="*性别">
					<u-radio-group v-model="radio" @change="change">
									<u-radio v-for="(item, index) in radioList" :key="index" :name="item.name" :disabled="item.disabled">
										{{ item.name }}
									</u-radio>
								</u-radio-group>
				</u-form-item>
				<u-form-item label="*手机号">
					<u-input v-model="form.phonenumber"></u-input>
				</u-form-item>
				<u-form-item label="*身份证">
					<u-input v-model="form.idCard"></u-input>
				</u-form-item>
				<u-form-item label="门诊类型" prop="address">
					<u-input v-model="form.address"></u-input>
				</u-form-item>
			</u-form>
		</view>
		<button class="bu" style="margin-top: 40rpx;" @click="submit">确定创建</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src: '',
				form:{
					userName: '',
					nickName: '',
					email: '',
					phonenumber: '',
					idCard: '',
					sex: '',
					avatar: '',
					address: '',
					birthday: '2023-03-01 10:30'
				},
				radio: '',
				radioList: [
					{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					}
				],
				rules: {
					birthday: [
						{
							required: true,
							message: '内容不能为空',
							trigger: ['blur','change']
						},
						{
							pattern: /\d{4}-\d{2}-\d{2}/,
							message: '日期格式不对',
							trigger: ['blur','change']
						}
					],
					address: [
						{
							required: true,
							message: '内容不能为空',
							trigger: ['blur','change']
						}
					]
				}
			};
		},
		onLoad() {
			this.getUser()
		},
		methods: {
			getUser(){
				this.$request({
					url: '/prod-api/api/common/user/getInfo'
				}).then(res => {
					this.form = res.user
					if(this.form.sex === '1'){
						this.radio = '男'
					}else{
						this.radio = '女'
					}
					this.form.idCard = this.form.idCard.replace(/(\w{2})\w*(\w{4})/, '$1************$2')
				})
			},
			submit(){
				this.$refs.uForm.validate(valid => {
					if(valid){
						this.$request({
							url: '/prod-api/api/hospital/patient',
							method: 'POST',
							data: {
								...this.form
							}
						}).then(res => {
							if(res.code === 200){
								uni.showToast({
									title:'创建成功'
								})
								setTimeout(() => {
									uni.navigateTo({
										url:'/pages/reserve/reserve'
									})
								}, 1000)
							}else{
								uni.showToast({
									title:res.msg,
									icon:'error'
								})
							}
						})
					}
				})
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		}
	}
</script>

<style lang="scss">
page{
	background-color: #f7f7f7;
	view{
		padding: 20rpx;
		.main{
			padding: 20rpx;
			background-color: #fff;
			border-radius: 20rpx;
		}
	}
}
</style>
